//
//  AllHouseViewController.m
//  RENTVISITOR
//
//  Created by wonder on 2017/9/7.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "AllHouseViewController.h"
#import "RentVieitor.h"
#import "AllHouseCell.h"
#import "OptionView.h"
#import "HouseDetailViewController.h"

@interface AllHouseViewController ()<UITableViewDelegate,UITableViewDataSource,LiaoOptionDeleget>

@end

@implementation AllHouseViewController{
    
    NSString *_identify;
    NSMutableArray *btnArray;
    OptionView *optinView;
    
}


- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BackgroundColor;
    btnArray = [NSMutableArray array];
    [self creatLeftView];
    [self creatView];
}


- (void)creatLeftView {
    
    // 自定义导航栏左侧按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(-5, 0, 10, 15);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"箭头左"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 自定义导航栏左侧按钮
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.frame = CGRectMake(5, 0, 30, 20);
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [rightBtn setTitle:@"取消" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor colorWithRed:99/255.0 green:99/255.0 blue:99/255.0 alpha:1] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(canceAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UIView *naView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, KWidth-70, 30)];
    naView.backgroundColor =  [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:229/255.0];
    naView.layer.masksToBounds = YES;
    naView.layer.cornerRadius = 2;
    self.navigationItem.titleView = naView;
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @" 房源标题  ";
    label.font = [UIFont systemFontOfSize:14];
    CGSize size = [@" 房源标题  " boundingRectWithSize:CGSizeMake(KWidth, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:nil].size;
    label.frame = CGRectMake(0, 5, size.width, 20);
    label.textColor = [Utils colorConvertFromString:@"#333333"];
    [naView addSubview:label];
    
    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(size.width, 2,naView.frame.size.width-size.width-20 , 26)];
    field.font = [UIFont systemFontOfSize:14];
    field.rightViewMode = UITextFieldViewModeAlways;
//    field.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:229/255.0];
    field.placeholder = @"请输入房源标题名称";
    field.layer.masksToBounds = YES;
    field.layer.cornerRadius = 2;
    [naView addSubview:field];
    
    UIButton *canceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    canceBtn.frame = CGRectMake(0, 0, 15, 15);
    [canceBtn setBackgroundImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
    [canceBtn addTarget:self action:@selector(moveAction) forControlEvents:UIControlEventTouchUpInside];
    field.rightView = canceBtn;
    
    
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 40)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
}

- (void)creatView {
    
    UITableView *_tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, KWidth, KHeight-50-64-49) style:UITableViewStylePlain];
    _tableview.showsVerticalScrollIndicator = NO;
    _tableview .showsHorizontalScrollIndicator = NO;
    _tableview.dataSource = self;
    _tableview.delegate = self;
    _identify = @"_Cell";
    [self.view addSubview:_tableview];
    [_tableview registerClass:[AllHouseCell class] forCellReuseIdentifier:_identify];
    UIButton *rank = [UIButton buttonWithType:UIButtonTypeCustom];
    [rank setBackgroundImage:[UIImage imageNamed:@"排序"] forState:UIControlStateNormal];
    [self.view insertSubview:rank aboveSubview:_tableview];
    
    [rank mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(-15);
        make.right.mas_equalTo(self.view.mas_right).with.offset(-17.5);
        make.size.mas_equalTo(CGSizeMake(50, 50));
        
    }];
    
    
    optinView = [[OptionView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 40) deleget:self];
    [optinView becomeFirstResponder];
    [self.view addSubview:optinView];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    AllHouseCell *cell = [tableView dequeueReusableCellWithIdentifier:_identify forIndexPath:indexPath];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 170*Main_Scale;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HouseDetailViewController *detailVc = [[HouseDetailViewController alloc] init];
    [self.navigationController pushViewController:detailVc animated:YES];
    
}



- (void)canceAction {
    
    optinView.state =@"yes";
    optinView.hide = @"yes";
    optinView.frame = CGRectMake(0, 0, KWidth, 40);
}

- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)moveAction {
    
}

- (void)tapAvatarView:(UIGestureRecognizer *)singer {
    
    singer.view.hidden = YES;
    
}

- (void)liaoOptionView:(OptionView *)option select:(NSString *)select {
    
//    optinView.frame = CGRectMake(0, 0, KWidth, KHeight);
}

- (void)liaoOptionView:(OptionView *)option changeFrame:(BOOL)isChange {
    
    if (isChange == YES) {
        optinView.frame = CGRectMake(0, 0, KWidth, 40);
    }else {
        optinView.frame = CGRectMake(0, 0, KWidth, KHeight);
    }
}

@end
